package com.restkeeper.service;
import com.restkeeper.entity.DishEs;
import com.restkeeper.entity.SearchResult;
import com.restkeeper.exception.BussinessException;
import com.restkeeper.util.EsSqlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
@Service(version = "1.0.0",protocol = "dubbo")
public class DishSearchServiceImpl implements IDishSearchService{

    @Value("${es.host}")
    private String host;

    @Value("${es.port}")
    private int port;

    @Override
    public SearchResult<DishEs> searchDishByCode(String code, int pageNum, int pageSize) {
        String shopId = RpcContext.getContext().getAttachment("shopId");
        if(StringUtils.isEmpty(shopId)){
            throw new BussinessException("商户号不存在");
        }
        String storeId= RpcContext.getContext().getAttachment("storeId");
        if(StringUtils.isEmpty(storeId)){
            throw new BussinessException("门店不存在");
        }
        //查询条件
        String condition="code like '%"+code+"%' and shop_id ='"+shopId+"' and store_id ='"+storeId+"' order by last_update_time desc";

        return EsSqlUtil.queryIndexContent(host,port,"dish",condition,pageNum,pageSize,DishEs.class);
    }

}